package com.wss.module.wan.ui.wxnumber.fragment;

import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.wss.common.base.BaseRefreshListFragment;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.bean.HorizontalTabTitle;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.utils.ToastUtils;
import com.wss.common.utils.ValidUtils;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.bean.WXNumber;
import com.wss.module.wan.ui.wxnumber.adapter.WXArticleAdapter;
import com.wss.module.wan.ui.wxnumber.mvp.WXArticlePresenter;
import com.wss.module.wan.ui.wxnumber.mvp.contract.WXArticleContract;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.Lifecycle;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe：公众号文章列表
 * Created by 吴天强 on 2018/11/15.
 */
public class WXArticleFragment extends BaseRefreshListFragment<WXArticlePresenter>
        implements WXArticleContract.View {

    private List<Article> wxArticles = new ArrayList<>();
    private int page = 0;

    @Override
    public void onViewCreated(Component view) {
        super.onViewCreated(view);
        init();
    }

    @Override
    protected void initView() {
        super.initView();
//        getPresenter().start();
    }

    private void resetPage() {
        page = 0;
        if (getAdapter() instanceof WXArticleAdapter) {
            ((WXArticleAdapter)getAdapter()).clear();
        }
        wxArticles.clear();
        getRefreshLayout().setIsShowLoadingMoreView(true);
    }

    public WXArticleFragment(Ability ability) {
        super(ability);
    }

    @Override
    protected BgarefreshLayout.BgarefreshLayoutDelegate createRefreshListener() {
        return new BgarefreshLayout.BgarefreshLayoutDelegate() {
            @Override
            public void onBgarefreshLayoutBeginRefreshing(BgarefreshLayout bgarefreshLayout) {
                resetPage();
                getPresenter().getArticle();
            }

            @Override
            public boolean onBgarefreshLayoutBeginLoadingMore(BgarefreshLayout bgarefreshLayout) {
                getPresenter().getArticle();
                return true;
            }
        };
    }

    @Override
    protected BaseItemProvider createAdapter() {
        return new WXArticleAdapter(context, wxArticles, new OnListItemClickListener<Article>() {
            @Override
            public void onItemClick(Article data, int position) {
                AbilityToAbility.toWebView(context, data.getLink());
            }
        });
    }

    @Override
    protected WXArticlePresenter createPresenter() {
        return new WXArticlePresenter();
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public int getWxId() {
        HorizontalTabTitle tabTitle = getTabTitle();
        System.out.println("==== get wx id by tab title : " + tabTitle.getTitle());
        if (ValidUtils.isValid(tabTitle) && ValidUtils.isValid(tabTitle.getData())) {
            return ((WXNumber) tabTitle.getData()).getId();
        }
        return -1;
    }

    @Override
    public void setTabTitle(HorizontalTabTitle data) {
        super.setTabTitle(data);
        resetPage();
//        getPresenter().getArticle();
    }

    @Override
    public void refreshWxArticle(List<Article> articleList) {

        this.wxArticles.addAll(articleList);
        if (getAdapter() instanceof WXArticleAdapter) {
            ((WXArticleAdapter)getAdapter()).addAll(wxArticles);
        }

        page++;
        stopRefresh();
    }

    @Override
    public void onError(Object tag, String errorMsg) {
        super.onError(tag, errorMsg);
        ToastUtils.show(errorMsg);
    }

    @Override
    public void onEmpty(Object tag) {
        super.onEmpty(tag);
        ToastUtils.show("暂无数据");
    }
}
